using System.Collections.Generic;
using Config;
using Data;
using Data.Bag;
using MoonFramework.Scripts.Event;
using MoonFramework.Scripts.Tool.System.Setting;
using MoonFramework.Scripts.View.UI;
using MoonFramework.Template;
using MoonFramework.Test;
using Tool;
using Tool.Name;
using UnityEngine;

namespace UI.Bag
{
    [UIElement(true, "UI/UI_ToolBarWindow", 1)]
    public abstract class UI_BagWindow : BaseUIWindow
    {
        protected BagData _bagData;
        [SerializeField] protected List<UI_ItemSlot> _slots; //物品槽
        public Sprite[] BgSprites; //背景图片

        #region 任务
        protected override void RegisterEvent()
        {
            EventCenter.Instance.Register<int>(EventName.AddItem);
        }

        protected override void SendEvent()
        {
            EventCenter.Instance.SendCommand<int>(EventName.AddItem, AddItem);
        }

        protected override void CancelEventListener()
        {
            EventCenter.Instance.RemoveCommand<int>(EventName.AddItem, AddItem);
        }
        #endregion

        //基于存档去初始化所有的格子
        protected virtual void InitSlotData()
        {
            for (int i = 0; i < _bagData?.ItemDataList.Length; i++)
            {
                _slots[i].Init(i, this);
                _slots[i].InitData(_bagData.ItemDataList[i]);
            }
        }

        #region 格子交互
        /// <summary>
        /// 添加一个物品
        /// </summary>
        public bool AddItemAndCheck(int itemConfigID)
        {
            bool ok = AddItemLogic(itemConfigID);
            ProjectTool.PlayAudio(ok ? PlayerAudioName.Bag : PlayerAudioName.Fail);
            return ok;
        }
        
        public void AddItem(int itemConfigID)
        {
            bool ok = AddItemLogic(itemConfigID);
            ProjectTool.PlayAudio(ok ? PlayerAudioName.Bag : PlayerAudioName.Fail);
        }

        /// <summary>
        /// 逻辑层添加物品
        /// </summary>
        /// <returns>是否成功</returns>
        public bool AddItemLogic(int itemConfigID)
        {
            ItemConfig itemConfig = ConfigManager.Instance.GetConfig<ItemConfig>(ConfigName.Item, itemConfigID);
            if (itemConfig.ItemType == ItemType.Weapon) return AddItemForEmptySlot(itemConfigID);
            return AddItemForSlot(itemConfigID) || AddItemForEmptySlot(itemConfigID);
        }

        protected bool AddItemForEmptySlot(int itemConfigID)
        {
            int index = GetEmptySlotIndex();
            if (index < 0) return false; //没有空格子
            SetItem(index, ItemData.CreateItemData(itemConfigID));
            return true;
        }

        protected int GetEmptySlotIndex()
        {
            for (int i = 0; i < _slots.Count; i++)
            {
                if (_slots[i].ItemData == null)
                    return i;
            }
            return -1;
        }
        
        /// <summary>
        /// 检查并且添加物品到指定格子
        /// </summary>
        private bool AddItemForSlot(int itemConfigID)
        {
            for (int i = 0; i < _bagData.ItemDataList.Length; i++)
            {
                if (_bagData.ItemDataList[i] != null && _bagData.ItemDataList[i].ConfigID == itemConfigID)
                {
                    BaseUsableItemData itemData = _slots[i].ItemData.ItemTypeData as BaseUsableItemData;
                    BaseUsableItemInfo itemInfo = _slots[i].ItemData.Config.ItemTypeInfo as BaseUsableItemInfo;
                    if (itemData.Count < itemInfo.MaxCount)
                    {
                        itemData.Count++;
                        _slots[i].UpdateCountTextView();
                        return true;
                    }
                }
            }
            return false;
        }
        #endregion

        /// <summary>
        /// 移除一个物品，而且不考虑是否堆积
        /// </summary>
        public virtual void RemoveItem(int index)
        {
            _bagData.RemoveItem(index);
            _slots[index].InitData(null);
        }

        public virtual void DiscardItem(int index)
        {
            ItemData itemData = _slots[index].ItemData;
            if(itemData == null) return;
            if (itemData.Config.ItemType == ItemType.Weapon)
            {
                RemoveItem(index);
            }
            else
            {
                BaseUsableItemData usableItemData = (BaseUsableItemData)itemData.ItemTypeData;
                usableItemData.Count--;
                if (usableItemData.Count <= 0)
                    RemoveItem(index);
                else
                    _slots[index].UpdateCountTextView();
            }
        }

        public virtual void SetItem(int index, ItemData itemData)
        {
            _bagData.SetItem(index, itemData);
            _slots[index].InitData(itemData);
        }

        public int GetItemCount(int configID)
        {
            int count = 0;
            foreach (var itemData in _bagData.ItemDataList)
            {
                if (itemData != null && itemData.ConfigID == configID)
                {
                    if (itemData.ItemTypeData is BaseUsableItemData data)
                        count += data.Count;
                    else count++;
                }
            }
            return count;
        }

        /// <summary>
        /// 基于建造配置 减少物品
        /// </summary>
        public void UpdateItemForBuild(BuildConfig buildConfig)
        {
            foreach (var condition in buildConfig.BuildConfigConditions)
            {
                UpdateItemForBuild(condition);
            }
        }

        protected void UpdateItemForBuild(BuildConfigCondition buildConfigCondition)
        {
            int count = buildConfigCondition.Count;
            int configID = buildConfigCondition.ItemID;
            for (int i = 0; i < _slots.Count; i++)
            {
                if (_slots[i].ItemData != null && _slots[i].ItemData.ConfigID == configID)
                {
                    if (_slots[i].ItemData.ItemTypeData is BaseUsableItemData usableItemData)
                    {
                        usableItemData.Count -= count;
                        if (usableItemData.Count >= 0)
                        {
                            if (usableItemData.Count > 0) _slots[i].UpdateCountTextView();
                            else RemoveItem(i);
                            return;
                        }
                        RemoveItem(i);
                    }
                    else
                    {
                        count--;
                        RemoveItem(i);
                        if (count == 0) return;
                    }
                }
            }
        }
    }
}